//
//  MKUIElementViewTextInput.m
//  MKUI
//
//  Created by 苏尚进 on 2023/7/13.
//  Copyright © 2023 moon. All rights reserved.
//

#import "MKUIElementViewTextInput.h"
#import "MKUIElementTextInputViewController.h"
#import "UIView+MKUI.h"
#import "MKUIPreCompile.h"

@implementation MKUIElementViewTextInput
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.accessView = self.disclosureIndicatorAccessView;
    }
    return self;
}
- (void)setText:(NSString *)text{
    if(_text==text||[_text isEqualToString:text])return;
    _text = text;
    [self __refreshDetailText];
}
- (void)setPlaceholder:(NSString *)placeholder{
    if(_placeholder==placeholder||[_placeholder isEqualToString:placeholder])return;
    _placeholder = placeholder;
    [self __refreshDetailText];
}
- (void)__refreshDetailText{
    self.detailText = self.text.length>0?self.text:self.placeholder;
    self.detailLabel.numberOfLines = self.allowsMultipleLineText?4:1;
}
- (void)setAllowsMultipleLineText:(BOOL)allowsMultipleLineText{
    if(_allowsMultipleLineText==allowsMultipleLineText)return;
    _allowsMultipleLineText = allowsMultipleLineText;
    [self __refreshDetailText];
}
- (CGSize)sizeThatFits:(CGSize)size{
    CGFloat height = [self titleAndSubTitleHeightThatFits:size];
    CGSize s2 = [self sizeThatFits:size contentSizeBlock:^CGSize(CGSize contentSize) {
        return [self.segLayoutConstraint sizeThatFits:contentSize resizeItems:YES];
    }];
    s2.height = MAX(height,s2.height);
    return s2;
}
- (void)beginTextInput{
    MKUIElementTextInputViewController *vc = [[MKUIElementTextInputViewController alloc] init];
    vc.doneButtonText = self.doneButtonText;
    vc.allowsMultipleLineText = self.allowsMultipleLineText;
    vc.title = self.titleText;
    vc.text = self.text;
    @MK_WEAKIFY(self);
    vc.whenChange = ^(__kindof MKUIElementTextInputViewController * _Nonnull vc, NSString * _Nonnull text) {
        @MK_NORMALIZE(self);
        if(self.whenChange){
            NSString *nextText = self.whenChange(self, text);
            vc.text = nextText;
        }
    };
    vc.whenDone = ^(__kindof MKUIElementTextInputViewController * _Nonnull vc) {
        @MK_NORMALIZE(self);
        [vc.mk_navigationControllerOfFirst popViewControllerAnimated:YES];
        self.text = vc.text;
        if(self.whenDone){
            self.whenDone(self);
        }
    };
    [self.mk_navigationControllerOfFirst pushViewController:vc animated:YES];
}
@end

@implementation MKUIElementListViewCellTextInput
+ (Class)elementViewClass{
    return MKUIElementViewTextInput.class;
}
- (void)didSelected:(BOOL)selected{
    [super didSelected:selected];
    if(!selected)return;
    [self.elementView beginTextInput];
}
@end

@implementation MKUIElementTableViewCellModelTextInput
- (void)setEl_allowsMultipleLineText:(BOOL)el_allowsMultipleLineText{
    self[@"el_allowsMultipleLineText"] = @(el_allowsMultipleLineText);
    self.el_elementView.allowsMultipleLineText = el_allowsMultipleLineText;
}
- (BOOL)el_allowsMultipleLineText{
    return [self mk_boolForKeyPath:@"el_allowsMultipleLineText"];
}
- (void)setEl_text:(NSString *)el_text{
    self[@"el_text"] = el_text;
    self.el_elementView.text = el_text;
}
- (NSString *)el_text{
    return self[@"el_text"];
}
- (void)setEl_placeholder:(NSString *)el_placeholder{
    self[@"el_placeholder"] = el_placeholder;
    self.el_elementView.placeholder = el_placeholder;
}
- (NSString *)el_placeholder{
    return self[@"el_placeholder"];
}
- (void)setEl_doneButtonText:(NSString *)el_doneButtonText{
    self[@"el_doneButtonText"] = el_doneButtonText;
    self.el_elementView.doneButtonText = el_doneButtonText;
}
- (NSString *)el_doneButtonText{
    return self[@"el_doneButtonText"];
}
- (void)setEl_whenChange:(NSString * _Nonnull (^)(__kindof MKUIElementTableViewCellModelTextInput * _Nonnull, NSString * _Nonnull))el_whenChange{
    self[@"el_whenChange"] = [el_whenChange copy];
}
- (NSString * _Nonnull (^)(__kindof MKUIElementTableViewCellModelTextInput * _Nonnull, NSString * _Nonnull))el_whenChange{
    return self[@"el_whenChange"];
}
- (void)setEl_whenDone:(void (^)(__kindof MKUIElementTableViewCellModelTextInput * _Nonnull))el_whenDone{
    self[@"el_whenDone"] = [el_whenDone copy];
}
- (void (^)(__kindof MKUIElementTableViewCellModelTextInput * _Nonnull))el_whenDone{
    return self[@"el_whenDone"];
}
@end

@implementation MKUIElementTableViewCellTextInput
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    return self;
}
+ (Class)elementViewClass{
    return [MKUIElementViewTextInput class];
}
- (void)customReloadCellModel{
    [super customReloadCellModel];
    MKUIElementTableViewCellModelTextInput *cm = self.cellModel;
    MKUIElementViewTextInput *elementView = self.elementView;
    elementView.allowsMultipleLineText = cm.el_allowsMultipleLineText;
    elementView.text = cm.el_text;
    elementView.placeholder = cm.el_placeholder;
    elementView.doneButtonText = cm.el_doneButtonText;
    @MK_WEAKIFY(self);
    elementView.whenChange = ^NSString * _Nonnull(__kindof MKUIElementViewTextInput * _Nonnull el, NSString * _Nonnull text) {
        @MK_NORMALIZE(self);
        MKUIElementTableViewCellModelTextInput *cm = self.cellModel;
        if(cm.el_whenChange){
            return cm.el_whenChange(cm,text);
        }
        return text;
    };
    elementView.whenDone = ^(__kindof MKUIElementViewTextInput * _Nonnull el) {
        @MK_NORMALIZE(self);
        MKUIElementTableViewCellModelTextInput *cm = self.cellModel;
        cm.el_text = el.text;
        if(cm.el_whenDone){
            return cm.el_whenDone(cm);
        }
    };
}
- (void)tableView:(UITableView *)tableView didSelectCell:(BOOL)selected{
    if(!selected) return;
    [self beginTextInput];
}
- (void)beginTextInput{
    MKUIElementViewTextInput *elementView = self.elementView;
    [elementView beginTextInput];
}
@end
